package com.useeinfo.oa.modules.maintaining.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.maintaining.enums.PlannerStatusEnum;
import com.useeinfo.oa.modules.maintaining.enums.WorkingHoursStatusEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.springframework.format.annotation.DateTimeFormat;
import sun.applet.Main;

import javax.persistence.*;
import java.util.Date;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/28 13:16
 * @Description: 工时管理
 */
@Entity
@Table(name = "working_hours")
public class WorkingHours extends SuperClassEntity<WorkingHours> {

    /**
     * 是否已完成
     */
    @Column(columnDefinition = "bit default 0")
    private Boolean completed = false;



    /**
     * 实际工时
     */
    private long actualWorkingHours;

    /**
     * 计划状态
     */
    @Enumerated(value = EnumType.STRING)
    @JoinColumn(name = "status")
    private WorkingHoursStatusEnum workingHoursStatusEnum;


    /**
     * 参与人 主参与人/辅助人
     */
    @ManyToOne
    @JoinColumn(name = "partner_id")
    private SystemUser partner;

    /**
     * 是否是主要负责人
     */
    @Column(columnDefinition = "bit default 0")
    private boolean isCharge;

    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 关联工单
     */
    @ManyToOne
    @JoinColumn(name = "maintain_order_id")
    private MaintainOrder maintainOrder;

    public Boolean getCompleted() {
        return completed;
    }

    public void setCompleted(Boolean completed) {
        this.completed = completed;
    }


    public long getActualWorkingHours() {
        return actualWorkingHours;
    }

    public void setActualWorkingHours(long actualWorkingHours) {
        this.actualWorkingHours = actualWorkingHours;
    }

    public WorkingHoursStatusEnum getWorkingHoursStatusEnum() {
        return workingHoursStatusEnum;
    }

    public void setWorkingHoursStatusEnum(WorkingHoursStatusEnum workingHoursStatusEnum) {
        this.workingHoursStatusEnum = workingHoursStatusEnum;
    }

    public SystemUser getPartner() {
        return partner;
    }

    public void setPartner(SystemUser partner) {
        this.partner = partner;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public MaintainOrder getMaintainOrder() {
        return maintainOrder;
    }

    public void setMaintainOrder(MaintainOrder maintainOrder) {
        this.maintainOrder = maintainOrder;
    }

    public boolean isCharge() {
        return isCharge;
    }

    public void setCharge(boolean charge) {
        isCharge = charge;
    }

    @Override
    public JSONObject toJSONObject(){
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("completed", StringConverters.null2Boolean(getCompleted())? "是" : "否");
        jsonObject.put("orderName",StringConverters.null2String(getMaintainOrder().getOrderName()));
        jsonObject.put("orderCode",StringConverters.null2String(getMaintainOrder().getOrderCode()));
        jsonObject.put("startDate",getMaintainOrder().getStartDateTime() == null ? "" : DateTimeUtils.format(getMaintainOrder().getStartDateTime(),"yyyy-MM-dd"));
        jsonObject.put("endDate",getMaintainOrder().getEndDateTime() == null ? "" : DateTimeUtils.format(getMaintainOrder().getEndDateTime(),"yyyy-MM-dd"));

        jsonObject.put("startTime",getMaintainOrder().getStartDateTime() == null ? "" : DateTimeUtils.format(getMaintainOrder().getStartDateTime(),"HH:mm:ss"));
        jsonObject.put("endTime",getMaintainOrder().getEndDateTime() == null ? "" : DateTimeUtils.format(getMaintainOrder().getEndDateTime(),"HH:mm:ss"));
        jsonObject.put("assign",getMaintainOrder().getAssign() ? "是" : "否");
        jsonObject.put("receiveUserName",getMaintainOrder().getReceiveUser() == null ? "":getMaintainOrder().getReceiveUser().getUserName());
        jsonObject.put("presence",getMaintainOrder().getPresenceTime() != null ? "是" : "否");
        jsonObject.put("receive",getMaintainOrder().getReceiveUser() != null ? "是" : "否");
        jsonObject.put("latitude",StringConverters.null2Long(getMaintainOrder().getLatitude()));
        jsonObject.put("longitude",StringConverters.null2Long(getMaintainOrder().getLongitude()));
        jsonObject.put("status",getWorkingHoursStatusEnum().getName());
        jsonObject.put("userName",getPartner().getUserName());
        jsonObject.put("workingHours",getActualWorkingHours());
        jsonObject.put("orderType",getMaintainOrder().isOrderType() ? "一次性" : "周期性");
        jsonObject.put("enterpriseName",StringConverters.null2String(getEnterprise().getEnterpriseName()));
        return jsonObject;
    }
}
